<template>
  <uni-nav-bar title="车队成员" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="package">
    <input class="uni-input" confirm-type="search" placeholder="搜索手机号/姓名" @confirm="confirm" v-model="search"
      @input="interrogate" />
  </view>
  <scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box"
    :style="{ height: pageHeight + 'px' }">
    <FriendList :frinedList="frinedList" :show="true" @del="del"></FriendList>
  </scroll-view>
  <view class="add" @click="navTo('administrator/fleet/addDriver')">新增</view>
</template>

<script setup lang="ts">
  import { ref } from 'vue';
  import { showLoading, errorToast, successToast } from '@/utils/prompt';
  import { navBack, navTo } from '@/utils/navigator';
  import FriendList from '@c/administrator/List/friendList.vue';
  import { toPublish } from '@mqtt';
  import { onShow } from '@dcloudio/uni-app';
  import { getFleetDriverList } from '@/gql/administrator';
  import { information } from '@/stores/administrator';
  const TeamInfo = information();
  const search = ref('')
  const inquireAbout = ref(false)
  const pageHeight = ref();

  const frinedList = ref([])
  uni.getSystemInfo({
    success: function (res) {
      pageHeight.value = res.windowHeight - 180;
    },
  });
  onShow(()=>{
    init()
  })
  function init() {
    showLoading()
    const payload = {
      query: getFleetDriverList,
      variables: {
        fleet_id: Number(TeamInfo.fleetAll.id),
        keywords: search.value
      },
    };
    toPublish(
      'ql/control/getFleetDriverList',
      payload,
      (obj : any) => {
        const { getFleetDriverList } = obj.data;
        frinedList.value = getFleetDriverList
      }
    );
  }
  /**
   * 搜索内容
   */
  function interrogate(evt : any) {
    const { detail } = evt;
    if (!detail.value && inquireAbout) {
      frinedList.value = [];
      init()
    }
  }
  /**
   * 搜索车友信息
   */
  function confirm() {
    if (search.value) {
      inquireAbout.value = search.value ? true : false;
      frinedList.value = [];
      init()
    } else {
      errorToast('请输入您要搜索的内容')
    }
  }
  function del(id : number,driverId:number) {
    showLoading()
    const payload = {
      fleet_id: Number(TeamInfo.fleetAll.id),
      driver_id: driverId,
    };
    toPublish(
      'control/delFleetDriver',
      payload,
      (obj : any) => {
        const { code, msg } = obj;
        if (code === 1) {
          successToast(msg)
          frinedList.value = frinedList.value.filter((item) => item.driver_id == id ? item = null : item)
        } else {
          errorToast(msg)
        }
      }
    );
  }
</script>

<style scoped lang="less">
  .add {
    width: 480rpx;
    height: 100rpx;
    background: linear-gradient(-10deg, #2C96F9, #1B91FF);
    border-radius: 5px;
    text-align: center;
    line-height: 100rpx;
    color: #fff;
    margin: 20rpx auto;
    font-size: 32rpx;
  }
</style>